﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Data.Common;
using Data.Enum;
using Data.Model;
using DataAccess.BikeManage;
using DataAccess.Common;
using Ninject;
using WinForm.Helper;

namespace WinForm.Dialog
{
    public partial class EditBike : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly IBike _bike = Kernel.Get<IBike>();
        private readonly ILockArea _lockArea = Kernel.Get<ILockArea>();
        private readonly ILookUpDataService _lookUpDataService = Kernel.Get<LookUpDataService>();

        public EditBike()
        {
            InitializeComponent();
        }

        public EditBike(int id)
        {
            InitializeComponent();

            if (id > 0)
            {
                lbBikeId.Text = id.ToString();
                Text = "编辑车辆信息";
                ResultInfo<bike> bike = _bike.GetBike(id);
                txtBikeCode.Text = bike.Obj.bikecode;
                txtBikePrice.Text = bike.Obj.price.ToString();
                txtMemo.Text = bike.Obj.memo;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(txtBikeCode.Text) || string.IsNullOrWhiteSpace(txtBikeCode.Text))
                {
                    MyMessageBox.ShowMessageBoxInfo("请输入车辆编号！");

                    return;
                }

                var bike = new bike
                {
                    bikeid = Converts.ConvertToInt(lbBikeId.Text),
                    price = Converts.ConvertToDecimal(txtBikePrice.Text),
                    bikecode = txtBikeCode.Text,
                    status =
                        Converts.ConvertToInt(cmbStatus.SelectedValue == null
                            ? ""
                            : cmbStatus.SelectedValue.ToString()),
                    type =
                        Converts.ConvertToInt(cmbType.SelectedValue == null ? "" : cmbType.SelectedValue.ToString()),
                    controlboxid =
                        Converts.ConvertToInt(cmbControlBox.SelectedValue == null
                            ? ""
                            : cmbControlBox.SelectedValue.ToString()),
                    lockpileid =
                        Converts.ConvertToInt(cmbLockAddress.SelectedValue == null
                            ? ""
                            : cmbLockAddress.SelectedValue.ToString()),
                    memo = txtMemo.Text
                };
                ResultInfo<int> id = _bike.CreateOrUpdateBike(bike);
                if (id.ResultNumber == ResultEnum.Success)
                {
                    if (string.IsNullOrEmpty(lbBikeId.Text))
                    {
                        MyMessageBox.ShowMessageBoxInfo("新增车辆信息成功！");
                    }
                    if (!string.IsNullOrEmpty(lbBikeId.Text))
                    {
                        MyMessageBox.ShowMessageBoxInfo("修改车辆信息成功！");
                    }

                    DialogResult = DialogResult.OK;
                }
                else
                {
                    MyMessageBox.ShowMessageBoxInfo(id.Describe);
                }
            }
            catch (Exception)
            {
                MyMessageBox.ShowMessageBoxInfo("保存失败！");
            }
        }

        private void EditLockPile_Load(object sender, EventArgs e)
        {
            try
            {
                ResultInfo<IList<jsh_Area>> areas = _lockArea.GetLockArea(0);
                if (areas.ResultNumber == ResultEnum.Success)
                {
                    cmbControlBox.DataSource = areas.Obj;
                    cmbControlBox.DisplayMember = "Area";
                    cmbControlBox.ValueMember = "id";

                    cmbLockAddress.DataSource = areas.Obj;
                    cmbLockAddress.DisplayMember = "Area";
                    cmbLockAddress.ValueMember = "id";
                }

                List<IdNameObject> status = _lookUpDataService.GetLockaddressStatus();
                cmbStatus.DataSource = status;
                cmbStatus.DisplayMember = "Name";
                cmbStatus.ValueMember = "Id";

                cmbType.SelectedIndex = 0;
            }
            catch (Exception)
            {
            }
        }

        private void btnPileCancel_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
        }
    }
}